/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import java.util.List;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import th.dao.KodFakultiDAO;
import th.dao.KodJantinaDAO;
import th.dao.KodJenisPengajianDAO;
import th.dao.KodJenisSekolahDAO;
import th.dao.KodNegeriDAO;
import th.dao.KodPengajianDAO;
import th.dao.KodSekolahDAO;
import th.dao.KodTahapPengajianDAO;
import th.dao.KodUniversitiDAO;
import th.dao.PoskodDAO;
import th.manager.ResponseManager;
import th.model.KodPengajian;
import th.model.KodSekolah;
import th.model.Poskod;
import th.model.Taha;
import th.stripes.BaseActionBean;
import th.stripes.services.TahaService;
import th.stripes.util.DateConverter;
import th.stripes.util.ListUtil;
import th.stripes.util.Util;
import th.stripes.webservice.WebServiceUtil;

/**
 *
 * @author ubuntu
 */
@UrlBinding("/taha")
public class TahaActionBean extends BaseActionBean{    
    
    
    @ValidateNestedProperties({ 
        @Validate(field="trhmasukIPT", converter=DateConverter.class),
        @Validate(field="trhtamatIPT", converter=DateConverter.class)
    }) 
    private Taha taha;
    
    private static String SUCCESSFUL = "0000";
    
    private static String SYSTEM_ERROR = "9999"; 
    
    private static String TAHA_ENQ_MSG_ID = "TPTAPRQRWD01";   
    
    private static String HEADER_ID_TAHA_ENQUIRY = "THSTAQR";    
    
    private List<KodSekolah> senaraiSekolah;
    
    private List<KodPengajian> senaraiPengajian;
    
    @Inject
    private Util util;
    
    @Inject 
    private ListUtil listUtil;
    
    @Inject
    private WebServiceUtil webUtil;   
    
    @Inject 
    private KodJantinaDAO kodJantinaDAO;
    
    @Inject 
    private ResponseManager responseManager;    
    
    @Inject 
    private KodUniversitiDAO kodUniversitiDAO;
    
    @Inject 
    private KodJenisPengajianDAO kodJenisPengajianDAO;
    
    @Inject 
    private KodTahapPengajianDAO kodTahapPengajianDAO;    
    
    @Inject 
    private KodSekolahDAO kodSekolahDAO;
    
    @Inject
    private KodNegeriDAO kodNegeriDAO;
    
    @Inject
    private KodJenisSekolahDAO kodJenisSekolahDAO;
    
    @Inject
    private PoskodDAO poskodDAO;
    
    @Inject
    private KodPengajianDAO kodPengajianDAO;
    
    @Inject
    private KodFakultiDAO kodFakultiDAO;
    
    @Inject
    private TahaService tahaService;
    
   private static Logger LOG = Logger.getLogger(TahaActionBean.class);
    
    
    @Before(stages={LifecycleStage.BindingAndValidation})
    public void rehydrate() {
      
    }
    
    @DefaultHandler
    public Resolution showMainForm() {
        return new JSP("taha/main_page.jsp");
    }
    
    public Resolution showFormPertanyaan() {
        return new JSP("taha/pertanyaan_taha_main.jsp");
    }
    
    public Resolution showPertanyaanDetail() {
        
        String responseCode = "";
        
        try { // Call Web Service Operation
               
             responseCode = tahaService.enquiryTaha(taha, responseCode);
            
            if ( responseCode.equals(SUCCESSFUL)) {
                 if (taha.getUmur() < 17) {
                      getContext().getRequest().setAttribute("age", "1");
                 } else {
                      getContext().getRequest().setAttribute("age", "2");
                 }
                               
                return new JSP("taha/pertanyaan_taha_submit.jsp");
            } else {
                addSimpleError("" + responseManager.getResponseMessage(responseCode, TAHA_ENQ_MSG_ID));  
            }
        } catch (Exception ex) {
            responseCode = SYSTEM_ERROR;
            addSimpleError("Terdapat masalah pada sistem.[ " + ex.toString() + " ]");
        }       
       return new JSP("taha/msg.jsp");
    }   
    
    
    public Resolution getSekolah() {
        String jenis_sekolah = getContext().getRequest().getParameter("jenis_sekolah");
        String sekolah_negeri = getContext().getRequest().getParameter("sekolah_negeri");              
        
        if (StringUtils.isBlank(sekolah_negeri) && StringUtils.isBlank(jenis_sekolah)){
            senaraiSekolah = listUtil.getSenaraiSekolah();
        } else {
            senaraiSekolah = listUtil.getSenaraiSekolah(jenis_sekolah, sekolah_negeri);
        }
        
        return new JSP("taha/sekolah_partition.jsp").addParameter("popup", "true");
    }
    
    public Resolution getPengajian() {
        String tahap_pengajian = getContext().getRequest().getParameter("tahap_pengajian");
        if (StringUtils.isBlank(tahap_pengajian) && StringUtils.isBlank(tahap_pengajian)){
            senaraiPengajian = listUtil.getSenaraiPengajian();
        } else {
            senaraiPengajian = listUtil.getSenaraiPengajian(tahap_pengajian);
        }        
        return new JSP("taha/pengajian_partion.jsp").addParameter("popup", "true");
    }
    
    public Resolution getPoskodDesc() {
        String result = "";
        String poskod = getContext().getRequest().getParameter("poskod");
        if (StringUtils.isNotBlank(poskod)) {
            Poskod pkod = poskodDAO.findById(poskod);
            if ( pkod != null ){
                result = pkod.getNama();
            }
        }
        return new StreamingResolution("text/plain", result);
    }

    public Taha getTaha() {
        return taha;
    }

    public void setTaha(Taha taha) {
        this.taha = taha;
    }

    public List<KodSekolah> getSenaraiSekolah() {
        return senaraiSekolah;
    }

    public void setSenaraiSekolah(List<KodSekolah> senaraiSekolah) {
        this.senaraiSekolah = senaraiSekolah;
    }  

    public List<KodPengajian> getSenaraiPengajian() {
        return senaraiPengajian;
    }

    public void setSenaraiPengajian(List<KodPengajian> senaraiPengajian) {
        this.senaraiPengajian = senaraiPengajian;
    }
    
    
}
